const DB = require('../core/DB')
const {
    Op
} = require('sequelize')

const {
    uMd5
} = require('../core/utils')


class UserController {

    /* ------------------------- ok 搜索用户 -------------------------------*/
    async searchUser(ctx) {
        // 参数
        let {
            page = 1, size = 10, id, status, name
        } = ctx.query

        page = Number(page)
        size = Number(size)

        let params = {}
        if (id) params.id = id
        if (status) params.status = status
        if (name) params.name = {
            [Op.like]: `%${name}%`
        }

        const {
            count,
            rows
        } = await DB.user.findAndCountAll({
            attributes: {
                exclude: ['password']
            },
            where: params,
            offset: (page - 1) * size,
            limit: size
        })

        const data = {
            rows,
            meta: {
                page: page, // 当前页数
                size, // 每页条目
                count: count, // 条目总数
                total: Math.ceil(count / size), // 总页数
            }
        }

        // if (count) {
        global.Response.success(ctx, undefined, data)
        // } else {
        //     global.Response.null(ctx)
        // }
    }

    /* ------------------------- ok 获取用户列表 -------------------------------*/
    async listUser(ctx) {
        // 参数
        let {
            page = 1, size = 10
        } = ctx.query

        page = Number(page)
        size = Number(size)

        const {
            count,
            rows
        } = await DB.user.findAndCountAll({
            attributes: {
                exclude: ['password']
            },
            offset: (page - 1) * size,
            limit: size
        })

        const data = {
            rows,
            meta: {
                page: page, // 当前页数
                size, // 每页条目
                count: count, // 条目总数
                total: Math.ceil(count / size), // 总页数
            }
        }

        if (count) {
            global.Response.success(ctx, undefined, data)
        } else {
            global.Response.null(ctx)
        }
    }

    /* ------------------------- ok 获取指定用户信息 -------------------------------*/
    async getUserInfo(ctx) {
        const {
            id
        } = ctx.query
        const user = await DB.user.findByPk(id, {
            attributes: {
                exclude: ['password']
            }
        })
        if (user) {
            global.Response.success(ctx, undefined, user)
        } else {
            global.Response.null(ctx)
        }
    }

    /* ------------------------- ok 获取登录用户信息 -------------------------------*/
    async getLoginInfo(ctx) {
        const {
            email
        } = ctx.query
        const user = await DB.user.findOne({
            where: {
                email
            },
            attributes: {
                exclude: ['password']
            }
        })
        if (user) {
            global.Response.success(ctx, email, user)
        } else {
            global.Response.null(ctx)
        }
    }


    /* ------------------------- ok 更新用户信息 -------------------------------*/
    async updateUser(ctx) {
        const {
            id,
            name,
            password,
            status
        } = ctx.request.body

        const user = await DB.user.findByPk(id)

        if (name) user.name = name
        if (password) user.password = uMd5(password)
        if (status != undefined) user.status = status
        user.updated_at = global.Moment().format('YYYY-MM-DD HH:mm:ss')
        user.save()

        global.Response.success(ctx, '更新成功')
    }

    /* ------------------------- 删除用户 -------------------------------*/
    async deteleUser(ctx) {
        const {
            id
        } = ctx.query

        const user = await DB.user.destroy({
            where: {
                id
            }
        })
        global.Response.success(ctx)

    }
}

module.exports = new UserController